package quq.missq.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.volley.VolleyError;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import quq.missq.BaseActivity;
import quq.missq.Constants;
import quq.missq.R;
import quq.missq.utils.MD5;
import quq.missq.utils.VolleyTool;

/**
 * Created by River on 8/10/15.
 */
public class ResetPaswordActivity2 extends BaseActivity implements View.OnClickListener{

    private EditText mPassword;
    private TextView mComplete;
    private String phone,password= "";
    private String code;
    private ProgressDialog dialog;
    private ImageView mBack;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reset_password_activity2);
        Bundle bundle = getIntent().getExtras();
//        phone = bundle.getString("phone");
//        code = bundle.getString("code");
        phone=getIntent().getStringExtra("phone");
        code=getIntent().getStringExtra("code");
        initView();
        initListener();

        Log.i("wcx", phone + "phone");
        Log.i("wcx",code+"code");

    }

    private void initView(){
        mPassword = (EditText)findViewById(R.id.password);
        mComplete = (TextView)findViewById(R.id.complete);
        mBack = (ImageView)findViewById(R.id.back);
    }

    private void initListener(){
        mComplete.setOnClickListener(this);
        mBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.complete){
            password = mPassword.getText().toString();
            if(TextUtils.isEmpty(password)){
                showToast("密码不能为空");
                return;
            }
            Map<String,String> params = new HashMap<>();
            password=MD5.getMD5(MD5.getMD5(password));
            params.put("password",password);
            params.put("verifyToken",code+"");
            params.put("mobile",phone);
            dialog = ProgressDialog.show(this, "提示", "发送中...", false, false);
            VolleyTool.post(this,Constants.FORGET_PASSWORD_URL,params,new VolleyTool.HTTPListener() {
                @Override
                public <T> void onResponse(T t, int requestCode) {
                    if(dialog != null){dialog.dismiss();}
                    try {
                        JSONObject obj = new JSONObject(t.toString());
                        int code = obj.getInt("code");
                        String message = obj.getString("message");
                        if(code >= 0){
                            showToast("密码修改成功");
                            setResult(RESULT_OK);
                            ResetPaswordActivity2.this.finish();
                        }else{
                            showToast(message);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onErrorResponse(VolleyError error, int requestCode) {
                    if(dialog != null){dialog.dismiss();}

                }
            },Constants.FORGET_PASSWORD,null);
        }
        if(v.getId() == R.id.back){
            this.finish();
        }
    }
}
